/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pojo;

import java.sql.ResultSet;
import java.util.ArrayList;
import util.MySQLDataAccessHelper;

/**
 *
 * @author Rocker000
 */
public class KhachHang_pojo 
{
    private int MaKhachHang;
    private String TenKhachhang;
    private String TenDangNhap;
    private String MatKhau;
    private String DiaChi;
    private String Email;
    private String SoDienThoai;
    private int IsAdmin;

    public KhachHang_pojo() {
    }

    public String getDiaChi() {
        return DiaChi;
    }

    public void setDiaChi(String DiaChi) {
        this.DiaChi = DiaChi;
    }

    public int getMaKhachHang() {
        return MaKhachHang;
    }

    public void setMaKhachHang(int MaKhachHang) {
        this.MaKhachHang = MaKhachHang;
    }

    public String getMatKhau() {
        return MatKhau;
    }

    public void setMatKhau(String MatKhau) {
        this.MatKhau = MatKhau;
    }

    public String getSoDienThoai() {
        return SoDienThoai;
    }

    public void setSoDienThoai(String SoDienThoai) {
        this.SoDienThoai = SoDienThoai;
    }

    public String getTenKhachhang() {
        return TenKhachhang;
    }

    public void setTenKhachhang(String TenKhachhang) {
        this.TenKhachhang = TenKhachhang;
    }

    public int getIsAdmin() {
        return IsAdmin;
    }

    public void setIsAdmin(int IsAdmin) {
        this.IsAdmin = IsAdmin;
    }

    public String getTenDangNhap() {
        return TenDangNhap;
    }

    public void setTenDangNhap(String TenDangNhap) {
        this.TenDangNhap = TenDangNhap;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }
    
    
    public boolean  TaoGioHang()
    {
        boolean kq = false;
        String sql = String.format("insert into giohang (UserName) values ('%s')",this.TenDangNhap);
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            int n = helper.ExeCuteUpdate(sql);
            if(n == 1)
                kq =true;
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return kq;
    }
    
    public int LayMaGioHang()
    {
        int ma=-1;
        String sql = String.format("select*from giohang where username = '%s'",this.TenDangNhap);
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
                ma = Integer.parseInt(rs.getString("MaGioHang"));
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return ma;
    }
    public ArrayList<ChiTietGioHang_pojo> LayChiTietGioHang()
    {
        ArrayList<ChiTietGioHang_pojo> ds = new ArrayList<ChiTietGioHang_pojo>();
        String sql = String.format("select*from ChiTietGioHang where magiohang = %d",this.LayMaGioHang());
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
            {
                ChiTietGioHang_pojo chitiet = new ChiTietGioHang_pojo();
                chitiet.setMachitiet(rs.getInt("machitiet"));
                chitiet.setMagiohang(rs.getInt("MaGioHang"));
                chitiet.setMasanpham(rs.getString("MaSanPham"));
                chitiet.setSoluong(rs.getInt("soluong"));
                ds.add(chitiet);
            }
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return ds;
    }
    
    public int KiemTraTonTaiChiTietGioHang()
    {
        int kq = 0;
        String sql = String.format("select*from ChiTietGioHang where magiohang = %d",this.LayMaGioHang());
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            if(rs.next())
            {
                 kq = 1;          
            }
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return kq;
    }
    
    public void XoaToanBoChiTietGioHang()
    {        
        String sql = String.format("delete from chitietgiohang where magiohang = %d",this.LayMaGioHang());
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            int n  = helper.ExeCuteUpdate(sql);
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
       
    }
    public void LuuChiTiet(ChiTietGioHang_pojo ct)
    {        
        String sql = String.format("insert into chitietgiohang (machitiet,masanpham,soluong,magiohang) values (%d,'%s',%d,%d)",ct.getMachitiet(),ct.getMasanpham(),ct.getSoluong(),ct.getMagiohang());
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            helper.ExeCuteUpdate(sql);
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }        
    }
    
    public void  LuuGioHang(ArrayList<ChiTietGioHang_pojo> giohang)
    {        
        for(int i = 0;i < giohang.size();i++)
        {
            this.LuuChiTiet(giohang.get(i));
        }        
    }
    
}
